<?php


namespace app\model;

use app\model\RemarkModel;
use think\Model;
use app\model\DeliveryModel;
use app\model\Attachment as AttachmentModel;
use app\model\QuotationTransportModel;
use app\model\QuotationModel;
use app\model\ConfigModel;
use think\facade\Db;
class DeliveryStepModel extends Model
{
    protected $table = "store_delivery_step";

    /**
     * @param $id
     * @return DeliveryStepModel[]|array|\think\Collection
     * @notes 订单进度
     * @author: dongdong
     * @date: 2025/1/11 17:40
     */
    public function info($id){
        $RemarkModel = new RemarkModel();
        $AttachmentModel = new AttachmentModel();
        $ConfigModel = new ConfigModel();
        try {
            $list = self::where(["delivery_id"=>$id])->order("id desc")->where("pid=0")->select();
            $arr = [];
            foreach ($list as $k =>&$v){
                $v['user'] = Db::connect("fast_mysql")->table("fa_user")->where(["id"=>$v['user_id']])->field("username,username_ru,mobile,level,avatar")->find();
                $v['user_info'] = Db::connect("fast_mysql")->table("fa_user_info")->where(["user_id"=>$v['user_id']])->field("identity_id,country,profile,profile_ru")->find();

                $v['identity'] = Db::connect("fast_mysql")->table("fa_identity")->where(["id"=>$v['user_info']['identity_id']])->field("name,name_ru")->find();
                $v['remark'] = $RemarkModel->info($v['remark_id']);
                $v['attachment'] = $AttachmentModel->lists($this->table,$v['attachment_id']);
                $v['image'] = $AttachmentModel->lists($this->table,$v['image_id']);
                $v['video'] = $AttachmentModel->lists($this->table,$v['video_id']);

                $v['area'] = Db::connect('fast_mysql')->table('fa_area')->where(["id"=>$v['area_id']])->find();
                $v['abnormal'] = $ConfigModel->info($v['abnormal_id']);

                $v['child'] = $this->child_row(self::where(["pid"=>$v['id']])->select());
                array_push($arr,$v);
            }
            return  $arr;
        }catch (\Exception $e){
            _d($e);
            return [];
        }
    }


    /**
     * @param $v
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @notes 子回复
     * @author: dongdong
     * @date: 2025/2/21 16:37
     */
    public function child_row($res){
        $RemarkModel = new RemarkModel();
        $AttachmentModel = new AttachmentModel();
        $ConfigModel = new ConfigModel();
        $arr = [];
        foreach ($res as $k =>&$v){
            if($v&&$v['user_id']){
                $v['user'] = Db::connect("fast_mysql")->table("fa_user")->where(["id"=>$v['user_id']])->field("username,username_ru,mobile,level,avatar")->find();
                $v['user_info'] = Db::connect("fast_mysql")->table("fa_user_info")->where(["user_id"=>$v['user_id']])->field("identity_id,country,profile,profile_ru")->find();

                $v['identity'] = Db::connect("fast_mysql")->table("fa_identity")->where(["id"=>$v['user_info']['identity_id']])->field("name,name_ru")->find();
                $v['remark'] = $RemarkModel->info($v['remark_id']);
                $v['attachment'] = $AttachmentModel->lists($this->table,$v['attachment_id']);
                $v['image'] = $AttachmentModel->lists($this->table,$v['image_id']);
                $v['video'] = $AttachmentModel->lists($this->table,$v['video_id']);

                $v['area'] = Db::connect('fast_mysql')->table('fa_area')->where(["id"=>$v['area_id']])->find();
                $v['abnormal'] = $ConfigModel->info($v['abnormal_id']);
                array_push($arr,$v);
            }
        }
        return $arr;
    }


    /**
     * @param $car_id
     * @return DeliveryStepModel|array|mixed|Model|null
     * @notes 最后打卡点
     * @author: dongdong
     * @date: 2025/1/20 14:03
     */
    public function last_step($car_id){
        try {
            $last_step = self::where(['car_id' => $car_id, 'type' => 0])->order("id desc")->find();
            if($last_step){
                $last_step['area'] = Db::connect('fast_mysql')->table('fa_area')->where(["id" => $last_step['area_id']])->find();
            }
            return $last_step;
        }catch (\Exception $e){
            return [];
        }
    }


    /**
     * @param $param
     * @return string
     * @notes 创建运踪
     * @author: dongdong
     * @date: 2025/2/21 17:39
     */
    public function create_step($param){
        Db::startTrans();
        try {
            $AttachmentModel = new AttachmentModel();

            $d['delivery_id'] = $delivery_id =  $param['delivery_id'];
            $d['type'] = 0;
            $d['status'] = $param['status']??1;
            $d['user_id'] = $param['user_id'];
            $d['create_at'] = date('Y-m-d H:i:s');
            $d['area_id'] = $param['area_id']??0;

            $d['pid'] = $param['pid']??0;

            $insertGetId = self::insertGetId($d);

            $RemarkModel = new RemarkModel();
            $remark_id = 0;
            if (!empty($param['remark'])) {
                $remark_id = $RemarkModel::remark_id($param['remark'], $this->table, $insertGetId);
            }

            $str = '';
            $attachment = '';
            $video = '';
            if (!empty($param['image'])) {
                $list_arr = explode(',', $param['image']);
                foreach ($list_arr as $k => &$v) {
                    $str .= $AttachmentModel->insertGetId(['user_id' => $param['user_id'], 'table_name' => $this->table, 'table_id' => $insertGetId, 'path' => $v]) . ',';
                }
                $str = trim($str, ',');
            }

            if (!empty($param['attachment'])) {
                $attachment_arr = explode(',', $param['attachment']);
                foreach ($attachment_arr as $k => &$v) {
                    $attachment .= $AttachmentModel->insertGetId(['user_id' => $param['user_id'], 'table_name' => $this->table, 'table_id' => $insertGetId, 'path' => $v]) . ',';
                }
                $attachment = trim($attachment, ',');
            }

            if (!empty($param['video'])) {
                $video_arr = explode(',', $param['video']);
                foreach ($video_arr as $k => &$v) {
                    $video .= $AttachmentModel->insertGetId(['user_id' => $param['user_id'], 'table_name' => $this->table, 'table_id' => $insertGetId, 'path' => $v]) . ',';
                }
                $video = trim($video, ',');
            }


            self::where(["id" => $insertGetId])->update(['remark_id' => $remark_id,'image_id'=>$str,'attachment_id'=>$attachment,'video_id'=>$video]);

            if($d['pid']){
                $row = self::where(["id"=>$d['pid'],'is_handle'=>0])->find();
                if($row){
                    self::where(["id"=>$d['pid']])->update(['is_handle'=>1]);
                    $delivery_id = self::where(["id" => $d['pid']])->value('delivery_id');
                    Db::query("update store_delivery set handle_count=handle_count+1 where id=$delivery_id");
                }
            }else{
                if(($d['status']==2)||($d['status']==3)) {
                    $delivery_id = $d['delivery_id'];
                    Db::query("update store_delivery set abnormal_count=abnormal_count+1 where id=$delivery_id");
                }
            }





            Db::commit();
            return '1';
        } catch (\Exception $e) {
            Db::rollback();
            return '请求失败';
        }
    }

    /**
     * @param $delivery_id
     * @return int
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @notes 是否拥有运踪
     * @author: dongdong
     * @date: 2025/2/21 17:41
     */
    public function is_have_step($delivery_id){
        $find = Db::table("store_delivery")->where("id=$delivery_id")->value('task_user_id');
        if($find){
            return 1;
        }else{
            return 0;
        }
    }
}